package com.xiangxiangjd.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiangxiangjd.bean.AdminUser;
import com.xiangxiangjd.bean.result.ResultPage;
import com.xiangxiangjd.controller.helper.$;
import com.xiangxiangjd.controller.model.ApiResultModel;
import com.xiangxiangjd.mapper.AdminUserMapper;
import com.xiangxiangjd.service.AdminUserService;
import com.xiangxiangjd.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 肖守凯
 * @since 2019-09-03
 */
@Service
public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUser> implements AdminUserService {



    @Autowired
    UserRoleService userRoleService;


    /**
     * 添加User
     *
     * @param adminUser 前端传进的参数
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel addUser(AdminUser adminUser) {
        //判断是否存在用户名
        QueryWrapper<AdminUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", adminUser.getEmail());
        if (list(queryWrapper).size() > 0) {
            return $.error("添加失败,账户名已存在");
        }

        if (this.save(adminUser)) {

            return $.success();
        } else {
            return $.error("添加失败");
        }
    }


    /**
     * 编辑跳转页面的回显数据User
     *
     * @param id 前端传进的参数
     * @return 返回实体类
     */
    @Override
    public AdminUser selectUserByIdUser(Integer id) {
        return this.getById(id);
    }


    /**
     * 编辑User
     *
     * @param adminUser 前端传进的参数
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel editUser(AdminUser adminUser) {
        if (this.updateById(adminUser)) {
            return $.success();
        } else {
            return $.error("编辑失败");
        }
    }

    @Override
    public ApiResultModel editPass(AdminUser adminUser, String code) {
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.set("pass_word", adminUser.getPassWord());
        wrapper.eq("email", adminUser.getEmail());
        if (update(wrapper)) {
            return $.success();
        }
        ;
        return $.error("操作失败");
    }

    /**
     * 删除一条User
     *
     * @param id 前端传进的ID
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel delUser(Integer id) {
        Map map = new HashMap();
        map.put("user_id", id);
        //删除 用户角色关联
        userRoleService.removeByMap(map);
        if (this.removeById(id)) {
            return $.success();
        } else {
            return $.error("删除失败");
        }
    }

    /**
     * 分页查询User
     *
     * @param page  页码
     * @param limit 查几条
     * @param adminUser  前端传进的参数
     * @return 返回统一分页信息
     */
    @Override
    public ResultPage findUserByPage(Integer page, Integer limit, AdminUser adminUser) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("page", (page - 1) * limit);
        paramMap.put("limit", limit);
        paramMap.put("user", adminUser);
        List<AdminUser> list = baseMapper.selectMyPage(paramMap);
        Integer count = baseMapper.countMyPage(paramMap);
        ResultPage<AdminUser> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

    @Override
    public AdminUser UserLogin(String requestUserName, String requestPassWord) {
        return baseMapper.userLogin(requestUserName, requestPassWord);
    }

    @Override
    public AdminUser findUserByUserId(Integer id) {
        return baseMapper.findUserByUserId(id);
    }


}
